<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
      label-width="80px">
      <el-form-item label="创建时间" prop="gmtCreate">
        <el-input v-model="dataForm.gmtCreate" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="修改时间" prop="gmtModified">
        <el-input v-model="dataForm.gmtModified" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="itemCode" prop="itemCode">
        <el-input v-model="dataForm.itemCode" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="商品名" prop="itemName">
        <el-input v-model="dataForm.itemName" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="商品描述" prop="itemDesc">
        <el-input v-model="dataForm.itemDesc" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="素材code" prop="itemMaterialCode">
        <el-input v-model="dataForm.itemMaterialCode" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="模板code" prop="itemTemplateCode">
        <el-input v-model="dataForm.itemTemplateCode" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="商品来源" prop="itemChannel">
        <el-input v-model="dataForm.itemChannel" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="商品图" prop="itemImageUrl">
        <el-input v-model="dataForm.itemImageUrl" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="扩展" prop="extInfo">
        <el-input v-model="dataForm.extInfo" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="环境" prop="env">
        <el-input v-model="dataForm.env" placeholder=""></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        gmtCreate: '',
        gmtModified: '',
        itemCode: '',
        itemName: '',
        itemDesc: '',
        itemMaterialCode: '',
        itemTemplateCode: '',
        itemChannel: '',
        itemImageUrl: '',
        extInfo: '',
        env: ''
      },
      dataRule: {
        gmtCreate: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        gmtModified: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        itemCode: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        itemName: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        itemDesc: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        itemMaterialCode: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        itemTemplateCode: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        itemChannel: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        itemImageUrl: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        extInfo: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        env: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/item/item/queryWormholeGalaxyItem`),
            method: 'post',
            data: this.$http.adornData({
              "id":id
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.gmtCreate = data.wmhGalaxyItem.gmtCreate
              this.dataForm.gmtModified = data.wmhGalaxyItem.gmtModified
              this.dataForm.itemCode = data.wmhGalaxyItem.itemCode
              this.dataForm.itemName = data.wmhGalaxyItem.itemName
              this.dataForm.itemDesc = data.wmhGalaxyItem.itemDesc
              this.dataForm.itemMaterialCode = data.wmhGalaxyItem.itemMaterialCode
              this.dataForm.itemTemplateCode = data.wmhGalaxyItem.itemTemplateCode
              this.dataForm.itemChannel = data.wmhGalaxyItem.itemChannel
              this.dataForm.itemImageUrl = data.wmhGalaxyItem.itemImageUrl
              this.dataForm.extInfo = data.wmhGalaxyItem.extInfo
              this.dataForm.env = data.wmhGalaxyItem.env
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/item/item/${!this.dataForm.id ? 'createWormholeGalaxyItem' : 'createWormholeGalaxyItem'}`),
            method: 'post',
            data: this.$http.adornData({
              'id': this.dataForm.id || undefined,
              'gmtCreate': this.dataForm.gmtCreate,
              'gmtModified': this.dataForm.gmtModified,
              'itemCode': this.dataForm.itemCode,
              'itemName': this.dataForm.itemName,
              'itemDesc': this.dataForm.itemDesc,
              'itemMaterialCode': this.dataForm.itemMaterialCode,
              'itemTemplateCode': this.dataForm.itemTemplateCode,
              'itemChannel': this.dataForm.itemChannel,
              'itemImageUrl': this.dataForm.itemImageUrl,
              'extInfo': this.dataForm.extInfo,
              'env': this.dataForm.env
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
